/*
 * @Date: 2023-02-16 14:33:05
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-19 16:57:12
 * @FilePath: \A19\backend\src\flowNodes\teacherOffline.ts
 */
import { WsConnection, WsServer } from "tsrpc";
import { ServiceType } from "../shared/protocols/serviceProto";
import { Test } from "../utils/Classes/Test";
import { Wsserver } from "..";
import { Live } from "../utils/Classes/Live";


export function onTeacherOffline(wsServer:WsServer<ServiceType>){
    wsServer.flows.postDisconnectFlow.push((v)=>{
        //老师退出监考
        const test = Test.tests.find(test => test.teacher?.connection.id === v.conn.id)
        if(test !== undefined){//老师是从监考退出
            test.teacher = undefined
            //通知学生客户端 教师离线 关闭rtc
            Wsserver.broadcastMsg('AMessages/Test/TeacherStatus',{
                status:'disconnected'
            },[...test.students.values()].map(student=>student.connection))
        }

        //教师退出直播
        const theLive = Live.lives.find(live => live.host.connection.id === v.conn.id)
        // let theLive:Live | undefined; 
        // console.log(theLive);
        // Live.lives.forEach(live =>{
        //     if(live.host.connection.id === v.conn.id) theLive = live
        // })
        if(theLive !== undefined){//老师是从直播退出
            theLive.endLive()
        }

        return v
    })
}